package com.pyramid.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class AppApi {

	private static final String URL = "http://58.64.151.200:8822/";
	private static com.pyramid.util.HashCache resultCache = null;
	private static AppApi instance;

	private AppApi() {

	}

	public static AppApi getInstance() {
		if (instance == null) {
			instance = new AppApi();
		}
		return instance;
	}

	/**
	 * @Title: doGet
	 * @Description: 从服务器获取数据
	 * @param @param url
	 * @return String 返回类型
	 * @throws
	 */
	public String doGet(String url) {
		String data = null;
		url = URL + url;
		if (resultCache != null) {
			data = resultCache.get(url);
			if (data != null) {
				return data;
			}
		}
		URI encodedUri = null;
		HttpGet httpGet = null;
		try {
			encodedUri = new URI(url);
			httpGet = new HttpGet(encodedUri);
		} catch (URISyntaxException e1) {
			// at least try to remove spaces
			String encodedUrl = url.replace(' ', '+');
			httpGet = new HttpGet(encodedUrl);
			e1.printStackTrace();
		}
		Log.e("", "encodedUri::: " + encodedUri);
		// initialize HTTP GET request objects
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse httpResponse = null;
		try {
			// execute request
			try {
				Log.e("", "httpGet::: " + httpGet);
				httpResponse = httpClient.execute(httpGet);
				Log.e("", "httpResponse::: " + httpResponse);
				if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					Log.e("", "JuWeiApi  connect  fail");
				}
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (SocketException e) {
				e.printStackTrace();
			}

			// request data
			HttpEntity httpEntity = httpResponse.getEntity();

			if (httpEntity != null) {
				InputStream inputStream = httpEntity.getContent();
				data = convertStreamToString(inputStream);
				// cache the result
				if (resultCache != null) {
					resultCache.put(url, data);
				}
			}

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return data;
	}

	/**
	 * @Title: convertStreamToString
	 * @Description: 浠庢祦涓鍙栨暟鎹繑鍥濻tring
	 * @param @param is
	 * @return String 杩斿洖绫诲瀷
	 * @throws
	 */
	private static String convertStreamToString(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}

	// /**
	// * @Title: getStatus
	// * @Description: 闁兼儳鍢茶ぐ鍥ㄦ交閺傛寧绀�柣妯垮煐閿燂拷
	// * @param @param data
	// * @return int 閺夆晜鏌ㄥú鏍尵鐠囪尙锟�
	// * @throws
	// */
	// public int getStatus(JSONObject result) {
	// int status = 0;
	//
	// try {
	// status = result.getInt("status");
	// } catch (JSONException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	// return status;
	// }
	// public String getError(JSONObject result){
	// String error=null;
	// try {
	// error=result.getString("error");
	// } catch (JSONException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return error;
	// }
	// public String getControl(JSONObject result){
	// String control=null;
	// try {
	// control=result.getString("control");
	// } catch (JSONException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return control;
	// }

}
